#include "CPlayer.h"

CPlayer::CPlayer() : CCharacter()
{
	m_CurrState				= ANIMATION_STATE_STAND;
	m_nNumSlash				= 0;
	m_nAmountTime			= 0;
	m_fAlpha				= 255.0f;
	m_fFixedPos				= 0.0f;
	m_fJumpTime				= 0.0f;
	m_fMaxJumpTime			= 1.0f/3.0f;
	m_fDashTime				= 0.0f;
	m_fMaxDashTime			= 1.0f/2.0f;
	m_fInvulnerableTime		= 0.0f;
	m_fMaxInvulTime			= 1.5f;
	m_isJump				= false;
	m_isDash				= false;
	m_isMove				= false;
	m_SlashBoundBox			= NULL;
	m_nSlashBoundBoxCount	= 0;
	SetDirection( DIR_RIGHT );
	ZeroMemory( &m_DashBoundBoxOffset , sizeof(CUSTOM_BOUND_BOX));
}

CPlayer::CPlayer(float Xpos, float Ypos, float deep, float XVelocity, float YVelocity, int width, int height):CCharacter( Xpos , Ypos , deep , XVelocity , YVelocity, width, height)
{
	m_CurrState				= ANIMATION_STATE_STAND;
	m_nNumSlash				= 0;
	m_nAmountTime			= 0;
	m_fAlpha				= 255.0f;
	m_fFixedPos				= 0.0f;
	m_fJumpTime				= 0.0f;
	m_fMaxJumpTime			= 1.0f/3.0f;
	m_fDashTime				= 0.0f;
	m_fMaxDashTime			= 1.0f/2.0f;
	m_fInvulnerableTime		= 0.0f;
	m_fMaxInvulTime			= 1.5f;
	m_isJump				= false;
	m_isDash				= false;
	m_isMove				= false;
	m_SlashBoundBox			= NULL;
	m_nSlashBoundBoxCount	= 0;
	SetDirection( DIR_RIGHT );
	ZeroMemory( &m_DashBoundBoxOffset , sizeof(CUSTOM_BOUND_BOX));
}

CPlayer::~CPlayer()
{
	if( m_SlashBoundBox )
		delete []m_SlashBoundBox;

	m_SlashBoundBox			= NULL;
	m_nSlashBoundBoxCount	= 0;
	ZeroMemory( &m_DashBoundBoxOffset , sizeof(CUSTOM_BOUND_BOX));
}





//----------------------------------//
// Name : Init						//
// Desc : Init attribute for player	//
//----------------------------------//
void CPlayer::Init(LPCTSTR DefineFile)
{
	char	Buffer[1025];
	char	Section[100];
	float	x , y , z;
	float	vx , vy;
	int		width , height;
	float	offsetX , offsetY;
	int		offsetWidth , offsetHeight;
	int		imageIndex;

	strcpy( Section , "Player" );

	//Get position 
	GetPrivateProfileString( Section , "Position" , "0, 0, 0" , Buffer , 1024 , DefineFile );
	sscanf( Buffer , "%f,%f,%f" , &x , &y , &z );

	SetPosition(x , y);
	SetDeep(z);

	//Get velocity
	GetPrivateProfileString( Section , "Velocity" , "0.0, 0.0" , Buffer , 1024 , DefineFile );
	sscanf( Buffer , "%f,%f" , &vx , &vy );

	SetVelocity( vx , vy );

	//Get size
	GetPrivateProfileString( Section , "Size" , "0, 0" , Buffer , 1024 , DefineFile );
	sscanf( Buffer , "%i,%i" , &width , &height );

	SetSize( width , height );

	//Get bounding box infomation
	GetPrivateProfileString( Section , "BoundBox" , "0.0, 0.0, 0, 0 " , Buffer , 1024 , DefineFile );
	sscanf( Buffer , "%f,%f,%i,%i" , &offsetX , &offsetY , &offsetWidth , &offsetHeight );

	CreateBoundBox( offsetX , offsetY , offsetWidth , offsetHeight );

	//Get attribute ( HP , MP , damage...)
	SetMaxAttribute( GetPrivateProfileInt( Section , "MaxHp" , 0 , DefineFile ) , GetPrivateProfileInt( Section , "MaxMp" , 0 , DefineFile ) );
	SetCurrAttribute( GetMaxHp() , GetMaxMp() );
	SetDamage( GetPrivateProfileInt( Section , "MinDamage" , 0 , DefineFile ) , GetPrivateProfileInt( Section , "MaxDamage" , 0 , DefineFile ) );
	SetLv( GetPrivateProfileInt( Section , "Level" , 0 , DefineFile ) );

	//Get Image index
	imageIndex = GetPrivateProfileInt( Section , "ImageIndex" , 0 , DefineFile );


	//----------------------------------//
	//	Get the Animation information	//
	//----------------------------------//
	strcpy( Section , "ZeroAnimation" );
	int numFrame = GetPrivateProfileInt( Section , "NumFrame" , 0 , DefineFile );
	int numFrameInRow = GetPrivateProfileInt( Section , "NumFrameInRow" , 0 , DefineFile );

	LoadSprite( &CResourceManager::GetInstance()->m_pListImage[imageIndex] , numFrame , numFrameInRow );

	int animCount = GetPrivateProfileInt( Section , "AnimCount" , 0 , DefineFile );
	int index , start , end , loop;
	float speedTop , speedBot;
	char keyName[100];
	CAnimation* pAnim = new CAnimation[animCount];

	for( ULONG i = 0 ; i < animCount ; ++i)
	{
		_stprintf( keyName , _T("Anim_%i") , i );
		GetPrivateProfileString( Section , keyName , "0, 0, 0, 0, 0.0/1.0" , Buffer , 1024 , DefineFile );
		sscanf( Buffer , "%i,%i,%i,%i,%f/%f" , &index , &start , &end , &loop , &speedTop , &speedBot );
		pAnim[i].CreateAnimation( index , start , end , loop , speedTop / speedBot );
	}

	GetSprite()->SetAnimation(pAnim);
	GetSprite()->SetAnimationIndex( ANIMATION_STATE_STAND );


	//--------------------------------------//
	//	Get the slash bound box information	//
	//--------------------------------------//
	strcpy( Section , "ZeroSlashBoundBox" );
	m_nSlashBoundBoxCount = GetPrivateProfileInt( Section , "NumBoundBox" , 0 , DefineFile );
	int offX , offY , sizeWidth , sizeHeight , offMove , relatedIndex;
	m_SlashBoundBox = new CUSTOM_BOUND_BOX[m_nSlashBoundBoxCount];

	for( ULONG i = 0 ; i < m_nSlashBoundBoxCount ; ++i )
	{
		_stprintf( keyName , _T("BoundBox_%i") , i );
		GetPrivateProfileString( Section , keyName , "0, 0, 0, 0, 0, 0" , Buffer , 1024 , DefineFile );
		sscanf( Buffer , "%i,%i,%i,%i,%i,%i" , &offX , &offY , &sizeWidth , &sizeHeight , &offMove , &relatedIndex );
		m_SlashBoundBox[i] = CUSTOM_BOUND_BOX( offX , offY , sizeWidth , sizeHeight , offMove , relatedIndex  );
	}
	
	//--------------------------------------//
	//	Get the dash bound box information	//
	//--------------------------------------//
	m_DashBoundBoxOffset.offsetX	= 10;
	m_DashBoundBoxOffset.width		= 60;
	m_DashBoundBoxOffset.offsetY	= 50;
	m_DashBoundBoxOffset.height		= 35;
	m_DashBoundBoxOffset.offsetMove	= 40;


	m_pWeapon = new WEAPON( "Dark_Sword" , 20 , 30 );

	m_pArmor = new ARMOR( "Dark_Armor" , 0 );
}



//--------------------------------------------------//
// Name : ChangeState								//
// Desc : Change to the given state and animation	//
//--------------------------------------------------//
void CPlayer::ChangeState(CPlayer::ANIMATION_STATE nextState)
{
	if( nextState >= NUM_OF_ANIMATION )
		return;
	m_CurrState = nextState;
	GetSprite()->SetAnimationIndex( m_CurrState );
}


//--------------------------------------------------//
// Name : SetState									//
// Desc : Set the current state for this Character	//
//--------------------------------------------------//
void CPlayer::SetState(CPlayer::ANIMATION_STATE state)
{
	if( state >= NUM_OF_ANIMATION )
		return;
	m_CurrState = state;
}



//--------------------------------------------------//
// Name : GetState									//
// Desc : Get the current state of this Character	//
//--------------------------------------------------//
CPlayer::ANIMATION_STATE CPlayer::GetState()
{
	return m_CurrState;
}



//--------------------------------------//
// Name : Moving						//
// Desc : Move the Character			//
//--------------------------------------//
void CPlayer::Moving( float deltaTime , DIRECTION dir )
{
	if( m_CurrState == ANIMATION_STATE_STAND ||
		m_CurrState == ANIMATION_STATE_WALK ||
		m_CurrState == ANIMATION_STATE_JUMP ||
		m_CurrState == ANIMATION_STATE_FALL || 
		m_CurrState == ANIMATION_STATE_END_DASH )
		{
			float moveX = 0.0f;
			if(dir == DIR_RIGHT)
			{
				if( GetCurrentDir() != dir )
				{
					SetXpos( GetXposition() + 40 );
					MoveBoundBox( -40 , 0 );
					m_DashBoundBoxOffset.offsetX -= m_DashBoundBoxOffset.offsetMove;
					MoveSlashBoundBox( true );
					GetSprite()->SetScale( 1.0f , 1.0f , D3DXVECTOR2( GetWidth() / 2.0f , GetHeight()  / 2.0f ) );
					SetDirection( dir );
					m_fFixedPos = 0.0f;
				}
				moveX = GetXVelocity() * deltaTime; 
			}
			else if( dir == DIR_LEFT )
			{
				if( GetCurrentDir() != dir )
				{
					SetXpos( GetXposition() - 40 );
					MoveBoundBox( 40 , 0 );
					m_DashBoundBoxOffset.offsetX += m_DashBoundBoxOffset.offsetMove;
					MoveSlashBoundBox( false );
					GetSprite()->SetScale( -1.0f , 1.0f , D3DXVECTOR2( GetWidth() / 2.0f , GetHeight()  / 2.0f ) );
					SetDirection( dir );
					m_fFixedPos = 40.0f;
				}
				moveX =  - GetXVelocity() * deltaTime; 
			}
			Move( moveX , 0 );
			
			COLLISIONCHECKING collisionCallbacks = (COLLISIONCHECKING)m_CallBack[CALLBACK_TYPE_COLLISION].pFunction;
			if( collisionCallbacks( this , m_CallBack[CALLBACK_TYPE_COLLISION].pContext ) )
			{
				Move( -moveX , 0 );
			}

			if(m_CurrState == ANIMATION_STATE_STAND || m_CurrState == ANIMATION_STATE_END_DASH )
			{
				ChangeState( ANIMATION_STATE_WALK );
			}
		}

	if( m_CurrState == ANIMATION_STATE_DASH || m_CurrState == ANIMATION_STATE_END_DASH )
	{
		if(dir == DIR_RIGHT)
		{
			if( GetCurrentDir() != dir )
			{
				m_isDash = true;
				ChangeState( ANIMATION_STATE_WALK );
				SetXpos( GetXposition() + 40 );
				MoveBoundBox( -40 , 0 );
				m_DashBoundBoxOffset.offsetX -= m_DashBoundBoxOffset.offsetMove;
				MoveSlashBoundBox( true );
				GetSprite()->SetScale( 1.0f , 1.0f , D3DXVECTOR2( GetWidth() / 2.0f , GetHeight()  / 2.0f ) );
				SetDirection( dir );
				m_fFixedPos = 0.0f;
			}
			if( !m_isMove)
			{
				ChangeState( ANIMATION_STATE_WALK );
				m_isDash = true;
			}
		}
		else if( dir == DIR_LEFT )
		{
			if( GetCurrentDir() != dir )
			{
				m_isDash = true;
				ChangeState( ANIMATION_STATE_WALK );
				SetXpos( GetXposition() - 40 );
				MoveBoundBox( 40 , 0 );
				m_DashBoundBoxOffset.offsetX += m_DashBoundBoxOffset.offsetMove;
				MoveSlashBoundBox( false );
				GetSprite()->SetScale( -1.0f , 1.0f , D3DXVECTOR2( GetWidth() / 2.0f , GetHeight()  / 2.0f ) );
				SetDirection( dir );
				m_fFixedPos = 40.0f;
			}
			if( !m_isMove)
			{
				ChangeState( ANIMATION_STATE_WALK );
				m_isDash = true;
			}
		}
	}
}



//--------------------------------------//
// Name : Standing						//
// Desc : Set the character standing	//
//--------------------------------------//
void CPlayer::Standing()
{
	if(m_CurrState != ANIMATION_STATE_WALK )
		return;
	ChangeState( ANIMATION_STATE_STAND );
	m_isMove = false;
}



//------------------------------//
// Name : Jumping				//
// Desc : Jump the character	//
//------------------------------//
void CPlayer::Jumping()
{
	if(m_CurrState != ANIMATION_STATE_FALL )
		SetYvelocity(-200);

	if(m_CurrState == ANIMATION_STATE_JUMP || m_CurrState == ANIMATION_STATE_FALL )
		return;

	m_isJump	= true;
	ChangeState( ANIMATION_STATE_JUMP );
}



//----------------------------------------------//
// Name : Falling								//
// Desc : Set the character to falling state	//
//----------------------------------------------//
void CPlayer::Falling()
{
	if(m_CurrState != ANIMATION_STATE_JUMP )
		return;

	m_isJump	= false;
	m_fJumpTime = 0.0f;
	ChangeState( ANIMATION_STATE_FALL );
}

//----------------------------------------------------------//
// Name : Landing											//
// Desc : Perform the landing animation for this character	//
//----------------------------------------------------------//
void CPlayer::Landing()
{
	if(m_CurrState != ANIMATION_STATE_LAND)
		return;

	if(GetSprite()->GetAnimation( ANIMATION_STATE_LAND )->GetCurrentFrameIndex() >= GetSprite()->GetAnimation(ANIMATION_STATE_LAND)->GetEndFrameIndex() )
	{
		ChangeState( ANIMATION_STATE_STAND );
		return;
	}
}



//--------------------------------------------------//
// Name : Slashing									//
// Desc : Changing our player state to slash state	//
//--------------------------------------------------//
void CPlayer::Slashing()
{
	if(m_CurrState == ANIMATION_STATE_END_SLASH ||
		m_CurrState == ANIMATION_STATE_FALL ||
		m_CurrState == ANIMATION_STATE_JUMP ||
		m_CurrState == ANIMATION_STATE_LAND ||
		m_CurrState == ANIMATION_STATE_DASH)
		return;
	if(m_CurrState == ANIMATION_STATE_STAND || m_CurrState == ANIMATION_STATE_WALK )
	{
		ChangeState( ANIMATION_STATE_SLASH );
	}
	if(m_CurrState == ANIMATION_STATE_SLASH && m_nNumSlash == 1)
	{
		ChangeState( ANIMATION_STATE_SECOND_SLASH );
	}
	if(m_CurrState == ANIMATION_STATE_SECOND_SLASH && m_nNumSlash == 2)
	{
		ChangeState( ANIMATION_STATE_FINAL_SLASH );
	}
}




//--------------------------------------//
// Name : Dashing						//
// Desc : Change our state to dashing	//
//--------------------------------------//
void CPlayer::Dashing( float deltaTime )
{
	if( m_isDash == false )
	{
		if( m_CurrState == ANIMATION_STATE_STAND || m_CurrState == ANIMATION_STATE_WALK )
		{
			if( m_CurrState == ANIMATION_STATE_WALK )
				m_isMove = true;
			ChangeState( ANIMATION_STATE_DASH );
			m_fDashTime = 0.0f;
		}

		if( m_CurrState == ANIMATION_STATE_DASH )
		{
			float moveX = 0.0;
			m_fDashTime += deltaTime;
			if( m_fDashTime >= m_fMaxDashTime )
			{
				ChangeState( ANIMATION_STATE_END_DASH );
				m_isDash = true;
				m_fDashTime = 0.0f;
				return;
			}
			if( GetCurrentDir() == DIR_RIGHT )
			{
				moveX = ( GetXVelocity() + 200 ) * deltaTime;
			}
			if( GetCurrentDir() == DIR_LEFT )
			{
				moveX = - ( GetXVelocity() + 200 ) * deltaTime;
			}
			Move( moveX , 0 );
			COLLISIONCHECKING collisionCallbacks = (COLLISIONCHECKING)m_CallBack[CALLBACK_TYPE_COLLISION].pFunction;

			if( collisionCallbacks( this , m_CallBack[CALLBACK_TYPE_COLLISION].pContext ) )
			{
				Move( - moveX , 0 );
				ChangeState( ANIMATION_STATE_END_DASH );
				m_isDash = true;
			}
		}
	}
}



//----------------------------------//
// Name : EndDash					//
// Desc : Update when end dashing	//
//----------------------------------//
void CPlayer::EndDash()
{
	m_isDash = false;
	if( m_CurrState == ANIMATION_STATE_DASH )
	{
		ChangeState( ANIMATION_STATE_END_DASH );
	}
}


//------------------------------------------//
// Name : UpdateSlash						//
// Desc : Update while our sprite is atking	//
//------------------------------------------//
void CPlayer::UpdateSlash()
{
	if( m_CurrState == ANIMATION_STATE_SLASH ||
		m_CurrState == ANIMATION_STATE_SECOND_SLASH ||
		m_CurrState == ANIMATION_STATE_FINAL_SLASH ||
		m_CurrState == ANIMATION_STATE_END_SLASH )
	{
		if( GetSprite()->GetAnimation(m_CurrState)->GetCurrentFrameIndex() == GetSprite()->GetAnimation(m_CurrState)->GetEndFrameIndex() )
		{
			if(m_CurrState == ANIMATION_STATE_END_SLASH)
			{
				ChangeState( ANIMATION_STATE_STAND );
				m_nNumSlash = 0;
				return;
			}
			m_nAmountTime++;
			if(m_nAmountTime > 10 )
			{
				ChangeState( ANIMATION_STATE_END_SLASH );
				m_nNumSlash = 0;
				m_nAmountTime = 0;
			}
			m_nNumSlash += 1;
		}
	}
}


//--------------------------------------//
// Name : Hurt							//
// Desc : Set player into hurt state	//
//--------------------------------------//
void CPlayer::Hurt( CCharacter *pCharacter )
{
	//TODO reduce player heal
	ChangeState( ANIMATION_STATE_HURT );
	SetColliable( false );
}


//----------------------------------//
// Name : UpdateHurt				//
// Desc : Update when player hurt	//
//----------------------------------//
void CPlayer::UpdateHurt( float deltaTime )
{
	static bool flag = false;
	if( m_CurrState == ANIMATION_STATE_HURT )
	{
		if( GetSprite()->GetAnimation(m_CurrState)->GetCurrentFrameIndex() == GetSprite()->GetAnimation(m_CurrState)->GetEndFrameIndex() )
		{
			ChangeState( ANIMATION_STATE_STAND );
		}
	}

	if( !IsColliable() )
	{
		m_fInvulnerableTime+= deltaTime;
		if( m_fInvulnerableTime >= m_fMaxInvulTime )
		{
			SetColliable();
			GetSprite()->SetColorAlpha( 255 );
			m_fInvulnerableTime = 0.0f;
			m_fAlpha = 255.0f;
			flag = false;
			return;
		}
		if( flag )
		{
			m_fAlpha += 10;
			if( m_fAlpha >= 255 )
			{
				flag = false;
			}
		}
		else
		{
			m_fAlpha -= 10;
			if( m_fAlpha <= 0 )
			{
				flag = true;
			}
		}
		GetSprite()->SetColorAlpha( m_fAlpha );
	}
}




void CPlayer::Update(float deltaTime)
{
	float jumpSpeed = 0;
	const float maxy = 300.0f;
	UpdateSlash();
	UpdateHurt( deltaTime );

	//Our character has a amount of time that he can jump , if time is larger , our character will jump higher 
	if(m_isJump)
	{
		m_fJumpTime += deltaTime;
		if(m_fJumpTime >= m_fMaxJumpTime )
		{
			Falling();
		}
	}
	if( m_CurrState == ANIMATION_STATE_END_DASH )
	{
		if( GetSprite()->GetAnimation(m_CurrState)->GetCurrentFrameIndex() >= GetSprite()->GetAnimation(m_CurrState)->GetEndFrameIndex() )
		{
			ChangeState( ANIMATION_STATE_STAND );
		}
	}
	
	//Check current Y velocity of our character to make sure he's not falling with high velocity
	if( GetYVelocity() >= maxy )
	{
		SetYvelocity(maxy);
	}

	//Gravity
	SetYvelocity( GetYVelocity() + GetGravity() * deltaTime);
	jumpSpeed = GetYVelocity() * deltaTime;
	
	//Move our character in Y-axis
	Move( 0 , jumpSpeed );

	//Checking collision
	COLLISIONCHECKING collisionCallbacks = (COLLISIONCHECKING)m_CallBack[CALLBACK_TYPE_COLLISION].pFunction;
	
	if( collisionCallbacks( this , m_CallBack[CALLBACK_TYPE_COLLISION].pContext ) )
	{
		Move( 0 , -jumpSpeed );
		while ( collisionCallbacks( this , m_CallBack[CALLBACK_TYPE_COLLISION].pContext ) )
		{
			if( jumpSpeed > 0 )
				Move( 0 , -1 );
		}
		//If we're in falling state , we hit ground , we change to landing
		if(m_CurrState == CPlayer::ANIMATION_STATE_FALL)
		{
			if(jumpSpeed > 0 )
			{
				ChangeState( ANIMATION_STATE_LAND );
			}
		}
		else if ( m_CurrState == ANIMATION_STATE_JUMP ) //If we're in jumping state, we hit some thing above , we change to falling
		{
			if(jumpSpeed < 0)
			{
				Falling();
				SetYvelocity(50);
			}
		}
		else if ( m_CurrState == ANIMATION_STATE_LAND )
		{
			Landing();
		}
	}
	else //If we're not collision, we're falling anyway
	{
		if(m_CurrState == ANIMATION_STATE_STAND || m_CurrState == ANIMATION_STATE_WALK || m_CurrState == ANIMATION_STATE_DASH )
		{
			m_CurrState = ANIMATION_STATE_JUMP;
			Falling();
		}
	}

	GetSprite()->Update(deltaTime);
}




//--------------------------------------------------------------------------------------------------//
// Name : Active (Callback function)																//
// Desc : If our character in seek zone of mobs , this method will make those mobs to active state	//
// *Active State == those mobs may attack player or go to defend etc...								//
//--------------------------------------------------------------------------------------------------//
bool CPlayer::Active(CMob *pMob, LPVOID pContext)
{
	RANGE seekRange; 
	pMob->GetSeekRange( &seekRange );

	if( ((CPlayer*)pContext)->GetXposition() >= seekRange.fMinRange && 
		((CPlayer*)pContext)->GetXposition() <= seekRange.fMaxRange )
	{
		return true;
	}

	return false;
}




//------------------------------------------------------------------------------//
// Name : GetFixedPos															//
// Desc : Our image cause a problem that when flip it has another X position	//
//		make camera change X position too										//
//		so this function fixed that bugs										//
//------------------------------------------------------------------------------//
float CPlayer::GetFixedXPos()
{
	return GetXposition() + m_fFixedPos;
}




//----------------------------------------------//
// Name : isCollision							//
// Desc : Checking collision with other object	//
//----------------------------------------------//
bool CPlayer::IsCollision( CObject* pObject )
{
	RECT playerRect = GetBoundBox();
	RECT mobRect	= pObject->GetBoundBox();

	if( m_CurrState == ANIMATION_STATE_SLASH ||
		m_CurrState == ANIMATION_STATE_SECOND_SLASH ||
		m_CurrState == ANIMATION_STATE_FINAL_SLASH )
	{
		RECT slashRect	= GetSlashBoundBox();

		if ( slashRect.top < mobRect.bottom && slashRect.bottom > mobRect.top 
			&& slashRect.left < mobRect.right && slashRect.right > mobRect.left )
		{
			((CMob*)pObject)->Hurt(this);
			CEffectManager::GetInstance()->StartEffect( CEffectManager::EFF_STRIKE , pObject->GetXposition(), pObject->GetYposition() , 0 , 0 , 0.30f );
		}
	}

	if ( playerRect.top < mobRect.bottom && playerRect.bottom > mobRect.top 
		&& playerRect.left < mobRect.right && playerRect.right > mobRect.left )
		return true;

	return false;
}




//--------------------------------------------------//
// Name : GetBoundBox								//
// Desc : Get the bound box of this animated object	//
//--------------------------------------------------//
RECT CPlayer::GetBoundBox()
{
	RECT r;
	//Fixed bound box when our player is dashing
	if( m_CurrState == ANIMATION_STATE_DASH )
	{
		if( GetSprite()->GetAnimation(m_CurrState)->GetCurrentFrameIndex() >= GetSprite()->GetAnimation(m_CurrState)->GetFrameLoopIndex() )
		{
			r.left		= GetXposition() + m_DashBoundBoxOffset.offsetX;
			r.right		= r.left + m_DashBoundBoxOffset.width * abs(GetSprite()->GetXScaleRatio());
			r.top		= GetYposition() + m_DashBoundBoxOffset.offsetY;
			r.bottom	= r.top + m_DashBoundBoxOffset.height * abs(GetSprite()->GetYScaleRatio());
			return r;
		}
	}

	r.left		= GetXposition() + m_fOffsetX;
	r.right		= r.left + m_nOffsetWidth * abs(GetSprite()->GetXScaleRatio());
	r.top		= GetYposition() + m_fOffsetY;
	r.bottom	= r.top + m_nOffsetHeight * abs(GetSprite()->GetYScaleRatio());

	return r;
}



//--------------------------------------//
// Name : GetSlashBoundBox				//
// Desc : Get the slashing bounding box	//
//--------------------------------------//
RECT CPlayer::GetSlashBoundBox()
{
	RECT r;
	ZeroMemory( &r , sizeof(RECT) );
	int currentIndex = GetSprite()->GetAnimation(m_CurrState)->GetCurrentFrameIndex();

	for( ULONG i = 0 ; i < m_nSlashBoundBoxCount ; ++i )
	{
		if( m_SlashBoundBox[i].relatedIndex == currentIndex )
		{
			r.left		= GetXposition() + m_SlashBoundBox[i].offsetX;
			r.right		= r.left + m_SlashBoundBox[i].width * abs(GetSprite()->GetXScaleRatio());
			r.top		= GetYposition() + m_SlashBoundBox[i].offsetY;
			r.bottom	= r.top + m_SlashBoundBox[i].height * abs(GetSprite()->GetYScaleRatio());
			return r;
		}
	}

	return r;
}


//------------------------------------------------------------------//
// Name : MoveSlashBoundBox											//
// Desc : Move slash bound box for some minor bug when flip player	//
//------------------------------------------------------------------//
void CPlayer::MoveSlashBoundBox( bool isOriginal /*= true*/)
{
	if( isOriginal )
	{
		for( ULONG i = 0 ; i < m_nSlashBoundBoxCount ; ++i )
		{
			m_SlashBoundBox[i].offsetX -= m_SlashBoundBox[i].offsetMove;
		}
	}
	else
	{
		for( ULONG i = 0 ; i < m_nSlashBoundBoxCount ; ++i )
		{
			m_SlashBoundBox[i].offsetX += m_SlashBoundBox[i].offsetMove;
		}
	}
}




int CPlayer::DamageCal()
{
	srand( timeGetTime() );
	int totalDamg	= 0;
	int minDamg		= GetMinDamage() + m_pWeapon->minDamage;
	int maxDamg		= GetMaxDamage() + m_pWeapon->maxDamage;

	if( m_CurrState == ANIMATION_STATE_SLASH )
	{
		int amount = (int)( (float)( maxDamg - minDamg ) * 0.3f );
		return rand() % amount + minDamg ;
	}
	if( m_CurrState == ANIMATION_STATE_SECOND_SLASH )
	{
		int amount = (int)( (float)( maxDamg - minDamg ) * 0.3f );
		return rand() % amount + ( amount + minDamg );
	}
	if( m_CurrState == ANIMATION_STATE_FINAL_SLASH )
	{
		int amount = (int)( (float)( maxDamg - minDamg ) * 0.7f );
		return rand() % amount + ( maxDamg - amount );
	}

	return 0;
}





